@echo off
chcp 65001 >nul
echo ====================================
echo   初始化数据库
echo ====================================
echo.

REM 检查 pnpm
where pnpm >nul 2>nul
if %errorlevel% neq 0 (
    echo [错误] 未找到 pnpm，请先安装: npm install -g pnpm
    pause
    exit /b 1
)

REM 检查依赖是否安装
if not exist "api\node_modules" (
    echo [提示] 检测到未安装依赖，正在安装...
    call pnpm run install:all
    if %errorlevel% neq 0 (
        echo [错误] 依赖安装失败
        pause
        exit /b 1
    )
)

echo [信息] 生成 Prisma Client...
pushd shared\prisma
if not exist "node_modules" (
    echo [信息] 在 shared\prisma 目录安装依赖...
    call pnpm install
    if %errorlevel% neq 0 (
        echo [错误] 依赖安装失败
        popd
        pause
        exit /b 1
    )
)
echo [信息] 生成 Prisma Client...
call npx prisma generate --schema=schema.prisma
if %errorlevel% neq 0 (
    echo [错误] Prisma Client 生成失败
    popd
    pause
    exit /b 1
)
popd

echo [信息] 运行数据库迁移...
pushd shared\prisma
call npx prisma migrate dev --name init --schema=schema.prisma
if %errorlevel% neq 0 (
    echo [错误] 数据库迁移失败
    popd
    pause
    exit /b 1
)
popd

echo.
echo [成功] 数据库初始化完成！
echo.
pause

